package com.weigee.ktv.m.observer;

import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by 林德 on 2017/4/5 10:30.
 * <p/>
 */
public class DrawerLayoutObserver implements Observer<Integer>
{
    public static final int COMPLETE = -1;
    public static final int LEFT_OPEN = 1;
    public static final int LEFT_CLOSE = 2;
    public static final int RIGHT_OPEN = 3;
    public static final int RIGHT_CLOSE = 4;

    private final DrawerLayout mDrawerLayout;
    private Disposable mDisposable;

    public DrawerLayoutObserver(DrawerLayout mDrawerLayout) {this.mDrawerLayout = mDrawerLayout;}

    @Override
    public void onSubscribe(Disposable d)
    {
        mDisposable = d;
    }

    @Override
    public void onNext(Integer direction)
    {
        if (mDrawerLayout == null) return;
        switch (direction)
        {
            case LEFT_OPEN:
                if (!mDrawerLayout.isDrawerOpen(Gravity.START))
                {
                    mDrawerLayout.openDrawer(Gravity.START);
                }
                break;
            case LEFT_CLOSE:
                if (mDrawerLayout.isDrawerOpen(Gravity.START))
                {
                    mDrawerLayout.closeDrawer(Gravity.START);
                }
                break;
            case RIGHT_OPEN:
                if (!mDrawerLayout.isDrawerOpen(Gravity.END))
                {
                    mDrawerLayout.openDrawer(Gravity.END);
                }
                break;
            case RIGHT_CLOSE:
                if (mDrawerLayout.isDrawerOpen(Gravity.END))
                {
                    mDrawerLayout.closeDrawer(Gravity.END);
                }
                break;
            case COMPLETE:
            default:
                if (mDisposable != null)
                {
                    mDisposable.dispose();
                }
        }
    }

    @Override
    public void onError(Throwable e)
    {
    }

    @Override
    public void onComplete()
    {
    }
}
